﻿using System;
using System.Data;
using System.Data.Linq;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;


namespace HieloCostalegre
{
    public partial class RegistrarVentas : System.Web.UI.Page
    {
        private HieloCostalegreDataContext _db = new HieloCostalegreDataContext();
        private List<Sale> _sales;
        private IEnumerable<Product> _products;
        private IEnumerable<Business> _businesses;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            Calendar.SelectedDate = DateTime.Now;
            LoadActiveRoutes();

        }

        protected void LoadActiveRoutes()
        {
            var activeRoutes = _db.Routes
                                    .Where(r => r.Active == true)
                                    .ToList();

            ddlRoutes.DataSource = activeRoutes;
            ddlRoutes.DataBind();
        }

        protected void RegisterSales(object sender, EventArgs e)
        {
            int customerId, productId, quantity;

            foreach (string key in Request.Form.AllKeys)
            {
                if (key.StartsWith("_sale|") == false)
                    continue;

                if(string.IsNullOrEmpty(Request.Form[key]))
                    continue;

                if (Int32.TryParse(Request.Form[key], out quantity) == false)
                    continue;

                string[] idValues = key.Split('|');
                customerId = Int32.Parse(idValues[1]);
                productId = Int32.Parse(idValues[2]);

                Sale sale = GetSale(customerId, productId);

                sale.Active = true;
                sale.CustomerId = customerId;
                sale.ProductId = productId;
                sale.Quantity = quantity;
                sale.Date = SaleDate;

                if (sale.Id == 0 && sale.Quantity > 0)
                    _db.Sales.InsertOnSubmit(sale);

                //if(sale.Quantity > 0)
                    _db.SubmitChanges();
            }

            Response.Redirect("RegistrarVentas.aspx");
        }

        private Sale GetSale(int customerId, int productId)
        {
            Sale sale;

            if (Sales.Where(s => s.CustomerId == customerId
                                && s.ProductId == productId
                                && s.Date == SaleDate)
                                .Any<Sale>())
            {
                sale = Sales.Where(s => s.CustomerId == customerId
                            && s.ProductId == productId
                            && s.Date == SaleDate)
                            .Single<Sale>();
            }
            else
                sale = new Sale();

            return sale;
        }

        protected string GetSaleQuantity(int businessId, int productId)
        {
            return GetSale(businessId, productId).Quantity.ToString();
        }

        public string GetSaleId(int businessId, int productId)
        {
            return string.Format("_sale|{0}|{1}", businessId, productId);
        }

        public IEnumerable<Business> Businesses
        {
            get
            {
                if (_businesses == null)
                {
                    _businesses = _db.Businesses
                        .Where(b => b.Route.Id == RouteId && b.Active == true)
                        .OrderBy(b => b.Name)
                        .ToList<Business>();
                }

                return _businesses;
            }
        }

        public IEnumerable<Product> Products
        {
            get
            {
                if (_products == null)
                {
                    _products = _db.Products
                        .Where(p => p.Active == true)
                        .OrderBy(p => p.Name)
                        .ToList<Product>();
                }

                return _products;
            }
        }

        public List<Sale> Sales
        {
            get
            {
                if(_sales == null)
                {
                    _sales = _db.Sales
                    .Where(s => s.Business.RouteId == RouteId
                        && s.Date == SaleDate
                        && s.Active == true)
                    .ToList<Sale>();
                }

                return _sales;
            }
        }

        public DateTime SaleDate
        {
            get
            {
                DateTime calendarDate = Calendar.SelectedDate;
                DateTime saleDate = new DateTime(calendarDate.Year, calendarDate.Month, calendarDate.Day);
                return saleDate;
            }
        }

        public int RouteId
        {
            get
            {
                int routeId;
                
                if (Int32.TryParse(ddlRoutes.SelectedValue, out routeId) == false)
                    routeId = 0;

                return routeId;
            }
        }

        protected void RouteChanged(object sender, EventArgs e)
        {
        }

        protected void DateChanged(object sender, EventArgs e)
        {
        }
    }
}
